﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using SuperMarketManagement.Entity;

namespace SuperMarketManagement.DAO.NHibernate
{
    public class ProductDAO : GenericDAO<Product, long>
    {
        public ProductDAO(ISession session)
            : base(session)
        {
        }

        public IList<Product> GetByCategory(Category category)
        {
            return Session.CreateCriteria(typeof(Product)).Add(Expression.Eq("Category.AutoId", category.AutoId)).List<Product>();
        }

        public IList<Product> FindByKeyword(string keyword)
        {
            string likePhrase = "%" + keyword + "%";
            return Session.CreateCriteria(typeof(Product))
                .Add(
                Expression.Or(
                    Expression.InsensitiveLike("Code", likePhrase),
                    Expression.InsensitiveLike("Name", likePhrase)
                    )
                ).List<Product>();
        }
    }
}
